/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.dian.common.vo.TreeVo;
import com.dian.common.exception.RRException;
import com.dian.modules.base.controller.UomController;
import  com.dian.common.enums.ValidEnum;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;

import com.dian.modules.base.dao.UomDao;
import com.dian.modules.base.entity.UomEntity;
import com.dian.modules.base.service.UomService;


@Service("baseUomService")
public class UomServiceImpl extends ServiceImpl<UomDao, UomEntity> implements UomService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UomEntity> page = this.page(
                new Query<UomEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    @Override
    public List<UomEntity> queryAll(Map<String, Object> params) {
       return  this.list(getQueryWrapper(params));
    }

    @Override
    public List<TreeVo<UomEntity>> queryTreeAll(Map<String, Object> params) {
        TreeVo<UomEntity> treeVo=new TreeVo<>();
        treeVo.setChildren( getChilds(0L));
        treeVo.setId(0L);
        treeVo.setTitle("计量单位");
        treeVo.setSpread(true);
        return   CollUtil.newArrayList(treeVo);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteInfo(Long id){

        int nCount=this.baseMapper.queryChildCount(id);
        if(nCount>0){
            throw new RRException(String.format("请先删除子数据"));
        }
        this.removeById(id);
        return true;
    }




    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/

    //获取子节点数据
    private List<TreeVo<UomEntity>> getChilds(Long uomId){
        List<TreeVo<UomEntity>> list=new ArrayList<>();
        QueryWrapper query= new QueryWrapper<UomEntity>();
        query.eq("p_id",uomId);
        query.eq("is_valid", ValidEnum.YES.getValue());
        List<UomEntity> listUomEntity=this.list(query);
        for (UomEntity baseUomEntity:listUomEntity){
            TreeVo<UomEntity> treeVo=new TreeVo<>();
            treeVo.setId(baseUomEntity.getId());
            treeVo.setTitle(baseUomEntity.getUomName());
            treeVo.setSpread(true);
            treeVo.setChildren(this.getChilds(baseUomEntity.getId()));
            list.add(treeVo);
        }
        return list;
    }

    /**
     * 获取查询条件
     *
     * @param
     */

    private QueryWrapper<UomEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<UomEntity> queryWrapper=new QueryWrapper<>();
        if(!StrUtil.isBlankIfStr(params.get("uomCode"))){
            queryWrapper.likeLeft("uom_code",params.get("uomCode"));
        }
        if(!StrUtil.isBlankIfStr(params.get("uomName"))){
            queryWrapper.likeLeft("uom_name",params.get("uomName"));
        }

        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }
}




